package com.xy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledService {

    @Autowired
    JavaMailSenderImpl mailSender;

    //cron表达式
    //秒 分 时 日 月 周几~
    //不确定周几，任何月任何号的15点55分2秒，每隔五秒执行一次
    @Scheduled(cron = "2/5 55 15 * * ?")
    public void hello(){
        System.out.println("定时任务-----");
    }

    //自定义练习,发送邮件任务
    ////不确定周几，任何月任何号的16点10分0秒发送了一封邮件
    @Scheduled(cron = "0 31 20 * * ?")
    public void myTest(){
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        Date date = new Date();
        String now = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss").format(date);
        //主题
        mailMessage.setSubject("定时任务");

        //内容
        mailMessage.setText(now+"学习SpringBoot定时任务时发送了一封邮件");

        mailMessage.setTo("1597719669@qq.com");
        mailMessage.setFrom("1597719669@qq.com");
        mailSender.send(mailMessage);
    }

}
